﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebBanHang
{
    public class MyCart
    {
        readonly List<CartItem> _items;
        public List<CartItem> Items
        {
            get { return _items; }
        }

        public MyCart()
        {
            _items = new List<CartItem>();
        }

        public void AddItem(CartItem item)
        {
            CartItem existedItem = _items.SingleOrDefault(c => c.ProductId == item.ProductId);
            if (existedItem != null)
            {
                existedItem.Quantity += item.Quantity;
            }
            else _items.Add(item);
        }

        public void UpdateItem(int productId, int quantity)
        {
            CartItem itemToUpd = _items.SingleOrDefault(c => c.ProductId == productId);
            itemToUpd.Quantity = quantity;
        }

        public void RemoveItem(int productId)
        {
            CartItem itemToDel = _items.SingleOrDefault(c => c.ProductId == productId);
            _items.Remove(itemToDel);
        }

        public int GetNumberOfItems()
        {
            return _items.Sum(c => c.Quantity);
        }
    }
}